/**
* ...
* @author Default
* @version 0.1
*/
import classes.data.XMLParser;
import classes.utils.VideoDownloader;
import classes.utils.SWFDownloader;

import mx.video.FLVPlayback;
import mx.transitions.Tween;
import mx.transitions.easing.Regular;

class classes.UpFront
{
	private static var m_objClassOwner;
	private var m_mcOwner:MovieClip;
	private var m_objXMLParser:XMLParser;
	private var m_objVideoDownloader:VideoDownloader;
	private var m_objSWFDownloader:SWFDownloader;
	private var m_flvPLayer:FLVPlayback;
	private var m_flvDownloader:FLVPlayback;
	private var m_intCurrentSegment:Number;
	private var m_intTotalSegments:Number;
	private var m_mcLoader:MovieClipLoader;


	public function UpFront(_mcOwner:MovieClip, _flvDownloader:FLVPlayback, _flvPlayer:FLVPlayback)
	{
		m_objClassOwner = this;
		m_mcOwner = _mcOwner;
		m_flvPLayer = _flvPlayer;
		m_flvDownloader = _flvDownloader;
		m_intCurrentSegment = 0;
		m_mcOwner.mcMoreInfo.btnClose._visible = false;

		m_objXMLParser = new XMLParser();
		m_objVideoDownloader = new VideoDownloader(_flvDownloader);
		m_objSWFDownloader = new SWFDownloader(_mcOwner);
		m_objXMLParser.addEventListener("complete", onXMLLoaded);

		m_mcLoader = new MovieClipLoader();



		createEvents();
	}

	private function createEvents():Void
	{

		var objThis = this;
		m_flvPLayer.playPauseButton = m_mcOwner.btnVideoPlayPause;
		m_flvPLayer.volumeBar = m_mcOwner.mcVideoVolumeBar;
		m_flvPLayer.muteButton = m_mcOwner.mcMuteButton;
		m_flvPLayer.seekBar = m_mcOwner.mcSeekBar;
		m_mcOwner.btnReplay.useHandCursor = false;

		var loadListener = new Object();

		m_mcOwner.btnReplay.onRelease = function()
		{
			m_flvPLayer.seek(0);
			m_flvPLayer.play();
		}

		/** Information box buttons */
		m_mcOwner.mcMoreInfo.btnInfo.onRelease = function ()
		{
			var mcMoreInfo = m_objClassOwner.m_mcOwner.mcMoreInfo;
			var tween:Object = new Tween(mcMoreInfo, "_y", Regular.easeOut, (mcMoreInfo._y), (mcMoreInfo._y - mcMoreInfo._height), 10, false);
			mcMoreInfo.btnInfo.enabled = false;
			tween.onMotionFinished = function ()
			{
				mcMoreInfo.btnClose._visible = true;
				new Tween(mcMoreInfo.btnClose, "_y", Regular.easeOut, (mcMoreInfo.btnClose._y), (mcMoreInfo.btnClose._y - mcMoreInfo.btnClose._height), 5, false);
			}
		}

		m_mcOwner.mcMoreInfo.btnClose.onRelease = function ()
		{
			var mcMoreInfo = m_objClassOwner.m_mcOwner.mcMoreInfo;
			var tween:Object = new Tween(mcMoreInfo.btnClose, "_y", Regular.easeOut, (mcMoreInfo.btnClose._y), (mcMoreInfo.btnClose._y + mcMoreInfo.btnClose._height), 5, false);
			tween.onMotionFinished = function ()
			{
				mcMoreInfo.btnInfo.enabled = true;
				mcMoreInfo.btnClose._visible = false;
				new Tween(mcMoreInfo, "_y", Regular.easeOut, (mcMoreInfo._y), (mcMoreInfo._y + mcMoreInfo._height), 10, false);
			}
		}

		loadListener.onLoadComplete = function(mcSource:MovieClip, httpStatus:Number):Void
		{
			objThis.m_mcOwner.mcProgressBar._visible = false;		
			objThis.m_mcOwner.mcSegmentContainer.gotoAndPlay(2);
		}

		loadListener.onLoadProgress   = function(target:MovieClip, bytesLoaded:Number, bytesTotal:Number)
		{
			var nPercent = (bytesLoaded/bytesTotal) * 100;
			objThis.m_mcOwner.mcProgressBar._xscale = nPercent;
		}

		m_mcLoader.addListener(loadListener);




	}

	private function loadSegment()
	{
		m_mcLoader.loadClip(m_objClassOwner.m_objXMLParser.currentSegmentPath ,m_mcOwner.mcSegmentContainer);
		m_mcOwner.mcProgressBar._visible = true;
	}


	private function onXMLLoaded (_event:Object):Void
	{
		/** Set default progress Label **/


		m_objClassOwner.m_mcOwner.txtSegmentTitle.text = m_objClassOwner.m_objXMLParser.defaultProgressLabel;

		/** Download Video **/
		for(var i:Number = 0; i<m_objClassOwner.m_objXMLParser.videoList.length; i++)
		{
			m_objClassOwner.m_objVideoDownloader.addVideoToQueue(m_objClassOwner.m_objXMLParser.videoList[i]);
		}
		m_objClassOwner.m_objVideoDownloader.startDownloadingList();

		/** Download SWFs **/
		for(var i:Number = 0; i < m_objClassOwner.m_objXMLParser.swfList.length; i++)
		{
			m_objClassOwner.m_objVideoDownloader.addSwfToQueue(m_objClassOwner.m_objXMLParser.swfList[i]);
		}

		//m_objClassOwner.m_objSWFDownloader.startDownloadingList();

		/** Add segment boxes **/
		m_objClassOwner.m_intTotalSegments = m_objClassOwner.m_objXMLParser.length;

		for(var i:Number = 0; i<m_objClassOwner.m_intTotalSegments; i++)
		{
			var mcSegmentBox:MovieClip = m_objClassOwner.m_mcOwner.mcSegmentBoxes.attachMovie("mcSegmentBox", ("mcSegmentBox" + i), i);
			mcSegmentBox._x = mcSegmentBox._width*i;
			mcSegmentBox.gotoAndStop(1);
			mcSegmentBox.index = i;

			/** rollover event for segment box **/
			mcSegmentBox.onRollOver = function ()
			{
				m_objClassOwner.m_mcOwner.txtSegmentTitle.text = m_objClassOwner.m_objXMLParser.getSegmentNameFor(this.index);
			}

			mcSegmentBox.onRollOut = mcSegmentBox.onReleaseOutside = function ()
			{
				m_objClassOwner.m_mcOwner.txtSegmentTitle.text = m_objClassOwner.m_objXMLParser.defaultProgressLabel;
			}
		}
		/** Set Current Segment Index for Data class **/
		m_objClassOwner.m_objXMLParser.currentSegmentIndex = m_objClassOwner.m_intCurrentSegment;

		/** change player looks as per requirement **/
		m_objClassOwner.setSeekBar();
		m_objClassOwner.setSegmentTitle();
		m_objClassOwner.setVideoControl(m_objClassOwner.m_objXMLParser.controlsRequired);
		m_objClassOwner.loadSegment();
	}

	private function setSeekBar():Void
	{
		var mcSegmentBox:MovieClip = m_mcOwner.mcSegmentBoxes["mcSegmentBox" + m_intCurrentSegment];
		mcSegmentBox.gotoAndStop(2);

		/** Reset All Segment Boxes to their original positions  **/
		for(var i:Number=0; i<m_intTotalSegments ; i++)
		{
			var mcSegmentBox1:MovieClip = m_mcOwner.mcSegmentBoxes["mcSegmentBox" + i];
			mcSegmentBox1._x = mcSegmentBox1._width * i;
			mcSegmentBox1._visible = true;
		}

		mcSegmentBox._visible = false;

		// Now place Seekbar to the current segment box position
		m_mcOwner.mcSeekBar._x = m_mcOwner.mcSegmentBoxes._x + mcSegmentBox._x;

		var intIntialX = m_mcOwner.mcSeekBar.mcPercentage._width - 10;

		for(var i:Number=m_intCurrentSegment; i<m_intTotalSegments ; i++)
		{
			mcSegmentBox = m_mcOwner.mcSegmentBoxes["mcSegmentBox" + i];
			mcSegmentBox._x = intIntialX + mcSegmentBox._width*i;
		}

		m_mcOwner.txtSegmentTitle._x =  m_mcOwner.mcSegmentBoxes._x + mcSegmentBox._x + mcSegmentBox._width;
	}

	private function setSegmentTitle():Void
	{
		m_mcOwner.mcHeader.txtHeaderTitle.text = m_objXMLParser.currentSegmentName;
	}

	private function setVideoControl(_isEnabled):Void
	{
		var colorTransform:Object = new Object();
		var objColors:Color;

		if(_isEnabled)
		{
			colorTransform.ra = 100;
			colorTransform.rb = 0;
			colorTransform.ga = 100;
			colorTransform.gb = 0;
			colorTransform.ba = 100;
			colorTransform.bb = 0;
			colorTransform.aa = 100;
			colorTransform.ab = 0;
		}
		else
		{


			colorTransform.ra = 0;
			colorTransform.rb = 102;
			colorTransform.ga = 0;
			colorTransform.gb = 102;
			colorTransform.ba = 0;
			colorTransform.bb = 102;
			colorTransform.aa = 100;
			colorTransform.ab = 0;





		}

		objColors = new Color(m_mcOwner.btnVideoPlayPause);
		objColors.setTransform(colorTransform);

		objColors = new Color(m_mcOwner.mcVideoVolumeBar);
		objColors.setTransform(colorTransform);

		objColors = new Color(m_mcOwner.mcMuteButton);
		objColors.setTransform(colorTransform);

		objColors = new Color(m_mcOwner.mcSeekBar);
		objColors.setTransform(colorTransform);

		objColors = new Color(m_mcOwner.btnReplay);
		objColors.setTransform(colorTransform);
	}

	public function set moreInfo(_strMoreInfo:String):Void
	{
		m_mcOwner.mcMoreInfo.txtDescription.htmlText = _strMoreInfo;
	}
	
	public function get moreInfo():String
	{
		return m_mcOwner.mcMoreInfo.txtDescription.text;
	}
	
	
	public function set segmentIndex(_intSegment:Number):Void
	{
		m_intCurrentSegment = _intSegment;
		loadSegment();
	}
	
	public function get segmentIndex():Number
	{
		return m_intCurrentSegment;
	}
}